package com.gexin.model.weixin;

import com.gexin.common.utils.SQLUtils;
import com.gexin.model.weixin.base.BaseUserAddress;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.util.LinkedList;
import java.util.List;

/**
 * 业务名称:  t_user_address(用户收货地址表) 
 */
@SuppressWarnings("serial")
public class UserAddress extends BaseUserAddress<UserAddress> {
	public static final UserAddress dao = new UserAddress();
	/**是否默认地址   FALSE*/
	public static final Integer DEFAULT_FALSE = 0;
	/**是否默认地址   TRUE*/
	public static final Integer DEFAULT_TRUE = 1;

	//根据user_id查找所有地址
	public List<UserAddress> findByUserAll(Integer user_id){
		return dao.find("select * from t_user_address where user_id = ? ",user_id);
	}


	public Page<UserAddress> query(int pageNumber,int pageSize,UserAddress address){
		List<Object> list =new LinkedList<Object>();
		SQLUtils sql=new SQLUtils(" from t_user_address where 1=1 ");
		if(address!=null){
			if(address.getUserId()!=null){
				sql.append(" and user_id = ? ");
				list.add(address.getUserId());
			}
		}
		sql.append(" order by id desc ");
		return paginate(pageNumber,pageSize,"select * ",sql.toString(),list.toArray());
	}

	public List<UserAddress> findByUserAlls(Integer user_id){
		return dao.find("select * from t_user_address where user_id = ?   order by is_default desc,createtime desc ",user_id);
	}

	public UserAddress findByUserDefault(Integer user_id){
		return dao.findFirst("select * from t_user_address where user_id = ?    and is_default =1 ",user_id);
	}


	public void setNotDefaultAll(Integer userId) {
		Db.update("UPDATE t_user_address SET is_default=0 where user_id=?",userId);
	}
}
